package com.huarun.common.util;

import java.io.PrintStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Base64.Decoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Crypt
{
  public static String publicKeyString = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvcqnaROg0V5b0VLdkPPYv36hbYrB49xS+9BymXI2oOxNEZjbhXjmDTMhNMkMXnCz5uOGHWQiQK2A3Qd6LNz/xfYS/8YVXLcM6GQ0hvDrmVXCBc+H9bTGwJWgehimBklXlidNXBFIHi9EEWmYV7PxlLviFlkoo0dI2FRBjemFKPKHl9rn2RmPSIiC2npdT1Il+s7UP3KhKuTSb9GPta6KKPgMxo+SZuwqIOk14hkDIYRPzWBEhX3J2DUa/F8y0HgLQNfVZcP11niEoHjJKFWtK2pHhnrrCeNIWfIWo5EtHrRUWs4jAhY40Auy1t41++r/mBTcMYNg/gqH+OsEsUbAQwIDAQAB";

  public static void main(String[] args)
  {
    String key = "owjjRRoSFZRGHZi0GKORYRSo0pADeTZN9yY1L9eBhLqXYBbgDP4ixWoVrqANI/NpjLgS3rBo7z5KjrNsVAlrRw/wkrRX305q1sS6tONkTk3yhq2DM9nIWmolwNINeqiMITyCTtNByftVC7EWjzZ+mm6fTjIHmMNtnhya44XWIM0sHMMm2aqUIBLiUt1rWxoT6UqnWT9OHEzEvGmTPB0bldnti4AcjS/JrZj31AEQk//TH5EQENXAQ6FdIOEMWB3IS9daIbBJLdZERaFeoKoH5pYYqdaCAnrwowE99MQkRNBI4lUua9l/eqIj+WTYUWLUdeACNOAuY49OoLs6oZwUQw==";
    String content = "goU75sWbqRx7MEnxMjHDmbAy01HEHdeiyZ7MTv7Txnv+cKejIyMO6q7s/uhtetbXth9zm//6FLH7wUjTmLbQJyuktRx5ZbWsUmqffl6QQajuubwAzuD/CepYg8Wmpun9ESIjI0maY+G7KVDzJVQ5xyKU3WcW4vDpJ9A2ENICwab7vuWnrBEO40iSeoCSERU/E0VQpyN86ARXzoU5aowa6pOoasokvUW6ujIluAOmz+IBirgnM/MAAAhUi+NWYeMaI8AP/5SdVy/P1BySOiiF3h0hk4lfLounj2m5/iY0Y6Ys1OMIT3ong1wfN5lSdb7IUrGZY+mKZ4XTOVPhLRBDaoMgstQaLedkhPuLWjUMTD/23j05cWRCE+XOTCjMeYV8e3Kp0+lbN4p7jtwRa+5pHLJRhBywIN+VEfMJRR6MiE5Pc6tGGro+qRgr89xBSvMQYOomW/MCGdt+dEsNPHylvAYW5VxTJkEKRrgyOEwIuJ7pJBxVR3diNZGuEt7pVXoKpcU81kMBf7dRrsqHwJXvDVq2dva27Sv9Wo9WgNU5YUNMT6r0+bbCGrUfn/8MfyICHevBSuQXW4pqcJPqmQjQg3RMa0jUGQjj0kEHy5cOC70IC12obS4HcnUCWt/IZv85T63NedlJNZsqWRX8mMRU4OYTrdInWBAUP7sgL7cWxY7WKC05KlxvMqrSYkaMSayUS+EGNr3fgJ7a8Ud3Kj5S2gAelVuKL3pqxypF119UE+3QA9cdL+AElplkLKGx4dmc+VcfRZNsPU2gH424N/dSWmcTJ+/dg0Tc98nS6lWBuD0wvV3QlZDPrx41zc3FQQMSIYY2d/QJbZRT/HHjVzw63w9eefGpaofFTdqkvopCdBO9sjvS18qD+aUqm1+Cvpkv3tbx8aR+EzPj0+UwGTovy6H6dAO6AFXsN5MIqUuuvDtxaXQESWOGFWqP01DW5hhJq6IETxnSllM40N8+f7y2Wg2lSoGoDnIdBct4NZ1mWNv7GM6phWpsX+b3xR/Iw+jI";
    try
    {
      PublicKey publicKey = getPublicKey(publicKeyString);

      byte[] keyBytes = Base64.getDecoder().decode(key);
      String keyTimeString = new String(decryptRSA(keyBytes, publicKey));
      String keyString = keyTimeString.substring(0, keyTimeString.length() - 13);
      Key aesKey = getSecretKey(keyString);
      System.out.printf("get key string %s\n", new Object[] { keyTimeString });
      System.out.printf("%s", new Object[] { keyString });

      byte[] msgBytes = Base64.getDecoder().decode(content);
      String plaintext = new String(decryptAES(msgBytes, aesKey));
      System.out.println(plaintext);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static byte[] decryptAES(byte[] encrypted, Key key)
    throws Exception
  {
    int ivSize = 16;

    byte[] iv = new byte[ivSize];
    System.arraycopy(encrypted, 0, iv, 0, iv.length);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

    int encryptedSize = encrypted.length - ivSize;
    byte[] encryptedBytes = new byte[encryptedSize];
    System.arraycopy(encrypted, ivSize, encryptedBytes, 0, encryptedSize);

    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(2, key, ivParameterSpec);
    return c.doFinal(encryptedBytes);
  }

  public static byte[] decryptRSA(byte[] encrypted, PublicKey publicKey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(2, publicKey);

    return cipher.doFinal(encrypted);
  }

  public static Key getSecretKey(String secKey) throws Exception {
    byte[] keyBytes = Base64.getDecoder().decode(secKey.getBytes());
    return new SecretKeySpec(keyBytes, "AES");
  }

  public static PublicKey getPublicKey(String publicKey) throws Exception {
    byte[] keyBytes = Base64.getDecoder().decode(publicKey.getBytes());
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return keyFactory.generatePublic(keySpec);
  }
}

/* Location:           D:\msg\反编译\Webuilding-0.0.1-SNAPSHOT(5)\BOOT-INF\classes\
 * Qualified Name:     com.huarun.common.util.Crypt
 * JD-Core Version:    0.6.2
 */